{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#导入模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#使用循环方法得到采购总额"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##定义for循环计算用的函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def veg_sum(X,Y):\n",
    "    # 定义日期列表\n",
    "    date=['7/28','7/29','7/30']    \n",
    "    # 定义起始值0\n",
    "    vegsum=[] \n",
    "    sum = 0\n",
    "    # 循环 \n",
    "    for i in range(3):\n",
    "        sum = 0\n",
    "        for j in range(3):\n",
    "            # 矩阵相乘 相加\n",
    "            sum += (X[i,j] * Y[j])\n",
    "        vegsum.append(sum)\n",
    "    print(vegsum)\n",
    "    for i in range(3):\n",
    "        print('日期:',date[i],'金额:',vegsum[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=np.array([[1.2,1.5,1.8],[1.3,1.4,1.9],[1.1,1.6,1.7]])\n",
    "Y=np.array([[5,10,9]]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([37.2]), array([37.6]), array([36.8])]\n",
      "日期: 7/28 金额: [37.2]\n",
      "日期: 7/29 金额: [37.6]\n",
      "日期: 7/30 金额: [36.8]\n",
      "[array([37.2]), array([37.6]), array([36.8])]\n",
      "日期: 7/28 金额: [37.2]\n",
      "日期: 7/29 金额: [37.6]\n",
      "日期: 7/30 金额: [36.8]\n",
      "[array([37.2]), array([37.6]), array([36.8])]\n",
      "日期: 7/28 金额: [37.2]\n",
      "日期: 7/29 金额: [37.6]\n",
      "日期: 7/30 金额: [36.8]\n",
      "[array([37.2]), array([37.6]), array([36.8])]\n",
      "日期: 7/28 金额: [37.2]\n",
      "日期: 7/29 金额: [37.6]\n",
      "日期: 7/30 金额: [36.8]\n",
      "[array([37.2]), array([37.6]), array([36.8])]\n",
      "日期: 7/28 金额: [37.2]\n",
      "日期: 7/29 金额: [37.6]\n",
      "日期: 7/30 金额: [36.8]\n",
      "[array([37.2]), array([37.6]), array([36.8])]\n",
      "日期: 7/28 金额: [37.2]\n",
      "日期: 7/29 金额: [37.6]\n",
      "日期: 7/30 金额: [36.8]\n",
      "[array([37.2]), array([37.6]), array([36.8])]\n",
      "日期: 7/28 金额: [37.2]\n",
      "日期: 7/29 金额: [37.6]\n",
      "日期: 7/30 金额: [36.8]\n",
      "[array([37.2]), array([37.6]), array([36.8])]\n",
      "日期: 7/28 金额: [37.2]\n",
      "日期: 7/29 金额: [37.6]\n",
      "日期: 7/30 金额: [36.8]\n",
      "The slowest run took 16.40 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
      "3.04 ms ± 3.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit res = veg_sum(X,Y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#使用矩阵点乘计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def veg_sum_dot(X,Y):\n",
    "    # 点乘\n",
    "    vegsum=np.dot(X,Y)\n",
    "    date=['7/28','7/29','7/30']\n",
    "    for i in range(3):\n",
    "        print('日期:',date[i],'金额:',vegsum[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "X=np.array([[1.2,1.5,1.8],[1.3,1.4,1.9],[1.1,1.6,1.7]])\n",
    "Y=np.array([5,10,9]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "日期: 7/28 金额: 37.2\n",
      "日期: 7/29 金额: 37.599999999999994\n",
      "日期: 7/30 金额: 36.8\n",
      "Wall time: 1.64 s\n"
     ]
    }
   ],
   "source": [
    "%time res=veg_sum_dot(X,Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
